<?php
namespace app\admin\controller;
class Worker extends Base
{
    //列表
    public function lists(){
        if(is_post()){
            $d = $this->post;
            $map = [];
            if($d['keyword']!=''){ $map[] = ['and','name|phone|area','like',"%{$d['keyword']}%"];}
            if($d['status']!='') $map[] = ['and','status','=',$d['status']];
            if($d['company_id']!='') $map[] = ['and','company_ids','like',"%,{$d['company_id']},%"];
            if($d['min_time']!='') $map[] = ['and','create_time','>=',strtotime($d['min_time'])];
            if($d['max_time']!='') $map[] = ['and','create_time','<=',strtotime($d['max_time'])];
            $w  = where($map);
            $sql = "
                SELECT
                    *
                FROM
                    worker
                $w[0]
                ORDER BY
                    sort
                DESC,
                    id
                DESC
            ";
            $rs = $this->db->query($sql.limit($d['page'],$d['limit']), $w[1],'',true);
            $company = $this->db->query('select * from company');
            if($rs){
                foreach($rs[0] as $k => $v){
                  $rs[0][$k]['status'] = c("type.status.{$v['status']}");
                  $rs[0][$k]['sex'] = c("type.status.{$v['sex']}");
                  $rs[0][$k]['create_time'] = com_time_format($v['create_time']);
                  //公司
                  $temp = "";
                  $company_ids = explode(",",$v['company_ids']);
                  foreach($company as $key => $val){
                    if(in_array($val['id'], $company_ids)){
                      $temp .= "{$val['title']},";
                    }
                  }
                  $rs[0][$k]['company_ids'] = $temp;
                }
                suc($rs[0],$rs[1]);
            }else{
                suc([],0);
            }
        }
        $company = $this->db->query('select * from company');
        return $this->fetch(['company'=>$company]);
    }

    //添加
    public function add(){
        if(is_post()){
            $d = $this->post;
            $rule = [
              'name' => "require",
              'company_ids' => "require",
            ];
            $msg = [
              'company_ids.require'=>'请选择所属公司',
            ];
            check($rule,$msg,$d);
            $d['create_time'] = time();
            $d['company_ids'] = isset($d['company_ids']) ? ",".implode(",",$d['company_ids'])."," : '';
            $d['age'] = (int)$d['age'];
            $d['sex'] = (int)$d['sex'];
            m('log','xcom')->record($this->userinfo['id'], "添加员工数据:".json_encode($d,JSON_UNESCAPED_UNICODE),"admin_user");
            $id = $this->db->insert('worker',$d);
            if ($id) {
                cookie('js_msg','添加成功');
                cookie('js_function','preload');
                return suc();
            }else{
                err('添加失败');
            }
        }
        $info = $this->db->query('select id,title from company');
        return $this->fetch(["info" => $info]);
    }
    //编辑
    public function edit(){
        if(is_post()){
            $d = $this->post;
            $rule = [
                'id'=>'require',
                'name' => "require",
                'age' => "require",
            ];
            $msg = [
            ];
            check($rule,$msg,$d);
            $d['update_time'] = time();
            $d['company_ids'] = isset($d['company_ids']) ? ",".implode(",",$d['company_ids'])."," : '';
            $d['age'] = (int)$d['age'];
            $d['sex'] = (int)$d['sex'];
            m('log','xcom')->record($this->userinfo['id'], "编辑员工数据:".json_encode($d,JSON_UNESCAPED_UNICODE),"admin_user");
            $id = $this->db->update('Worker',$d);
            if ($id) {
                cookie('js_msg','修改成功');
                cookie('js_function','preload');
                return suc();
            }else{
                err('修改失败');
            }
        }
        $id = $this->get['id'];
        $group_list = $this->db->query('select id,title from company');
        $sql = "SELECT id,name,phone,age,sex,address,area,status,note,score,company_ids FROM worker WHERE id=?";
        $info = $this->db->query($sql,[$id],1);
        $checked = explode(",",$info["company_ids"]);
        return $this->fetch(['group_list'=>$group_list,'info'=>$info,"checked"=>$checked]);
    }


    //删除
    public function del(){
        if(is_post()){
            $d = $this->post;
            $this->db->delete('worker',$d['id']);
            cookie('js_msg','删除成功');
            cookie('js_function','close|reload');
            return suc();
        }
    }

    #批量删除
    function delall(){
      if(is_post()){
            $d = $this->post;
            $ids = $d['id'];
            foreach($ids as $id){
                $this->db->delete('worker',$id);
            }
            cookie('js_msg','删除成功');
            cookie('js_function','close|reload');
            return suc();
      }
    }

    //修改上下架
    public function updateStatus(){
      if(is_post()){
            $d = $this->post;
            $status = $this->get('status');
            $ids = $d['id'];
            foreach($ids as $id){
                $this->db->update('worker',['status'=>$status],['id=?',[$id]]);
            }
            cookie('js_msg','修改成功');
            cookie('js_function','reload');
            return suc();
      }
    }

    //公司详情
    public function detail(){
        $id = $this->get("id");
        $info = $this->db->query("select * from worker where id = ?",[$id],1);
        $this->fetch(['info'=>$info]);
    }
}
